<?= "<?php\n" ?>
declare(strict_types=1);

namespace <?= $namespace ?>Model;

use think\Model;

/**
 * <?= $tableComment ?: $modelName . ' Model' ?>
<?php if (!empty($tableComment)): ?>
 * 
 * Table: <?= $tableName ?> - <?= $tableComment ?>
<?php endif; ?>
 * 
<?php if (!empty($fields)): ?>
<?php foreach ($fields as $field): ?>
 * @property <?php
        switch ($field['type']) {
            case 'boolean':
                echo 'bool';
                break;
            case 'integer':
            case 'biginteger':
                echo 'int';
                break;
            case 'decimal':
            case 'float':
                echo 'float';
                break;
            case 'json':
                echo 'array';
                break;
            case 'datetime':
            case 'timestamp':
            case 'date':
            case 'time':
                echo 'string';
                break;
            default:
                echo 'string';
        }
    ?> $<?= $field['name'] ?><?= !empty($field['comment']) ? ' ' . $field['comment'] : '' ?>
<?php endforeach; ?>
<?php endif; ?>
 * @package <?= $namespace ?>Model
 */
class <?= $modelName ?> extends Model
{
    /**
     * 获取模型选项配置
     * 
     * @return array
     */
    protected function getOptions(): array
    {
        return [
            // 表名
            'name' => '<?= $tableName ?>',
            
            // 主键
            'pk' => 'id',
            
            // 自动时间戳
            'auto_timestamp' => 'datetime',
            
            // 创建时间字段
            'create_time' => 'create_time',
            
            // 更新时间字段
            'update_time' => 'update_time',
            
            // 软删除时间字段
            'delete_time' => false,
<?php if (!empty($fields)): ?>
            
            // 字段类型转换
            'type' => [
<?php foreach ($fields as $field): ?>
<?php if ($field['type'] === 'boolean'): ?>
                '<?= $field['name'] ?>' => 'boolean',
<?php elseif ($field['type'] === 'integer' || $field['type'] === 'biginteger'): ?>
                '<?= $field['name'] ?>' => 'integer',
<?php elseif ($field['type'] === 'decimal' || $field['type'] === 'float'): ?>
                '<?= $field['name'] ?>' => 'float',
<?php elseif ($field['type'] === 'json'): ?>
                '<?= $field['name'] ?>' => 'json',
<?php elseif (in_array($field['type'], ['datetime', 'timestamp'])): ?>
                '<?= $field['name'] ?>' => 'datetime',
<?php elseif ($field['type'] === 'date'): ?>
                '<?= $field['name'] ?>' => 'date',
<?php endif; ?>
<?php endforeach; ?>
            ],
<?php endif; ?>
<?php if (!empty($fillableFields)): ?>
            
            // 允许批量赋值的字段
            'field' => [
<?php foreach ($fillableFields as $field): ?>
                '<?= $field ?>',
<?php endforeach; ?>
            ],
<?php endif; ?>
<?php if (!empty($requiredFields)): ?>
            
            // 验证规则
            'validate' => [
                'rule' => [
<?php foreach ($requiredFields as $field): ?>
                    '<?= $field['name'] ?>' => 'require<?= isset($field['unique']) && $field['unique'] ? '|unique:' . $tableName : '' ?>',
<?php endforeach; ?>
                ],
                'message' => [
<?php foreach ($requiredFields as $field): ?>
                    '<?= $field['name'] ?>.require' => '<?= $field['comment'] ?: $field['name'] ?>不能为空',
<?php if (isset($field['unique']) && $field['unique']): ?>
                    '<?= $field['name'] ?>.unique' => '<?= $field['comment'] ?: $field['name'] ?>已存在',
<?php endif; ?>
<?php endforeach; ?>
                ],
            ],
<?php endif; ?>
        ];
    }
    
    /**
     * 搜索器：根据状态查询
     */
    public function searchStatusAttr($query, $value)
    {
        if ($value !== '') {
            $query->where('status', $value);
        }
    }
    
    /**
     * 搜索器：根据创建时间范围查询
     */
    public function searchCreateTimeAttr($query, $value)
    {
        if (is_array($value) && count($value) === 2) {
            $query->whereBetweenTime('create_time', $value[0], $value[1]);
        }
    }
    
<?php if (!empty($relationshipFields)): ?>
<?php foreach ($relationshipFields as $relation): ?>
    /**
     * 关联查询：<?= $relation['comment'] ?>
     */
    public function <?= $relation['method'] ?>()
    {
        return $this->belongsTo(<?= $relation['model'] ?>::class, '<?= $relation['foreign_key'] ?>', '<?= $relation['local_key'] ?>');
    }
    
<?php endforeach; ?>
<?php endif; ?>
}